#ifndef __TIMER_UTILS__
#define __TIMER_UTILS__

#include <sys/time.h>
#include "elog.h"
#include "elog_file.h"

struct time_checker {
    timeval start_time;
    timeval stop_time;

    void start()
    {
        gettimeofday(&start_time, nullptr);
    }
    void stop()
    {
        gettimeofday(&stop_time, nullptr);
    }
    int distance()
    {
        long long time_1_token = (long long)start_time.tv_sec * 1000 + start_time.tv_usec / 1000;
        long long time_2_token = (long long)stop_time.tv_sec * 1000 + stop_time.tv_usec / 1000;
        return time_2_token - time_1_token;
    }
    void show_distance(const char*title = "current time")
    {
        long long time_1_token = (long long)start_time.tv_sec * 1000 + start_time.tv_usec / 1000;
        long long time_2_token = (long long)stop_time.tv_sec * 1000 + stop_time.tv_usec / 1000;
        log_i("%s : %lld ms", title, time_2_token - time_1_token);
    }
    void show_us_distance(const char*title = "current time")
    {
        long long time_1_token = (long long)start_time.tv_sec * 1000000 + start_time.tv_usec;
        long long time_2_token = (long long)stop_time.tv_sec * 1000000 + stop_time.tv_usec;
        log_i("%s : %lld us", title, time_2_token - time_1_token);
    }
    bool timeout(int second)
    {
        timeval current;
        gettimeofday(&current, nullptr);
        long long time_1_token = (long long)start_time.tv_sec * 1000 + start_time.tv_usec / 1000;
        long long time_2_token = (long long)current.tv_sec * 1000 + current.tv_usec / 1000;
        long long value = time_2_token - time_1_token;
        return value > second;
    }
    static long long get_current_timetoken()
    {
        timeval current;
        gettimeofday(&current, nullptr);
        return ((long long)current.tv_sec * 1000 + current.tv_usec / 1000);
    }
};

#endif
